Attribute VB_Name = "Conf"
Public Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long

'change 2014-10-31
Type SettingConf
     Message As String
     ItemNum As Integer
     Index As Integer 'this element denotes the corresponding index of first item in array DevConf()
     Port As String
     SCode As String
End Type

Public DevConf() As New ItemConf
'save full TestItems
Public ExeConf() As New ItemConf
'save TestItems that are to be Measured.
Public ConnectConf() As SettingConf
'save port information
Public ConnectNum As Integer
'save the number of  real TestItems which to be measured
Public ItemNum As Integer
'save the number of items to be measured
Public ActualItemNum As Integer
'save Config File Pathname
Public pathName As String
'all test ports
Public AllTestPorts() As Integer
'save the array length of AllTestPorts()
Public TestPortsNum As Integer

Public WriteIndex() As String
'save the write order for the reporte file
Public RealWriteIndex() As String
Public Message As String
Public Current_State As String
Public CallSta_Time As Single
Public iniPath As String
Public IniFileName As String 'save the current config file name
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

Public Function InitConf(tempPathName As String)
    pathName = tempPathName
End Function

'If ReadConf correctly exe
'return number of test item(eg 13 item)
'else return -1
Public Function ReadConf()

End Function
Public Function DevTest_origin() 'Conduct ExeConfig

   ' Dim ExeResult As Integer
  '  Dim StartPortNum As Integer
   ' StartPortNum = 0
    'Dim StopPortNum As Integer
   ' StopPortNum = TestPortsNum - 1
   ' Dim I As Integer
   ' While (StopPortNum >= StartPortNum)
       ' For I = StartPortNum To StopPortNum
          '  ExeResult = MeasPort(StartPortNum, I)
       ' Next I
       ' StartPortNum = StartPortNum + 1
       ' I = StopPortNum
       ' While I >= StartPortNum
          '  ExeResult = MeasPort(I, StopPortNum)
         '   I = I - 1
       ' Wend
        'StopPortNum = StopPortNum - 1
        'ExeResult=MeasPort(StopPortNum-StarPortNum,
   ' Wend
End Function
Public Function SelectItem(Items() As Integer, ItemNum As Integer)
    ReDim ExeConf(ItemNum)
    For i = 0 To ItemNum
        ExeConf(i) = DevConf(Items(i))
    Next i
End Function

Public Function MatchNum(Aray() As ItemConf, name As String, FindTime As Integer) As Integer
    Dim FindCnt As Integer
    FindCnt = 0
    For i = 0 To ItemNum - 1
        If name = Aray(i).ItemName Then
           FindCnt = FindCnt + 1
           If FindCnt = FindTime Then
              MatchNum = i
              Exit Function
           End If
        End If
    Next i
    MsgBox "No matched element can be found!"
End Function

'check whether the port in AllTestPorts() array
Public Function SetTure(Start As Integer, Length As Integer, ch As Integer, state As String)
For i = Start To Start + Length - 1
    If DevConf(i).ch = ch And DevConf(i).Sta = state And DevConf(i).MeasType <> Meas_IL Then
       DevConf(i).IsSwept = True
    End If
Next i
End Function
Public Function CheckDir(fileName As String) As Boolean
      If Dir(fileName, vbDirectory) <> "" And fileName <> "" Then
         CheckDir = True
      Else
         CheckDir = False
      End If
End Function
'return the file path which ".ini" is excluded
Public Function FiltIni(str As String) As String
        Dim Length As Integer
        Length = ToIniLen(str)
        If Length <= 0 Then
            MsgBox "Wrong file path!", vbOKOnly + vbQuestion, "Error!"
            Exit Function
        End If
        FiltIni = Left(str, Length - 1)
End Function
'return char num before reaching ".ini"
Public Function ToIniLen(str As String) As Integer
       ToIniLen = InStr(str, ".ini")
End Function
'This function is designed to abstract ini file name in the selected path
Public Function FiltAll(str As String) As String
       
    Dim temp As String
    Dim tempArray() As String
    temp = FiltIni(str)
    Dim Length As Integer
    Length = Len(temp)
    'substract the string:"D:\ATE\Config"
    'If Length <= 14 Then
    '   MsgBox "Wrong file path!", vbOKOnly + vbQuestion, "Error!"
    '   Exit Function
    'End If
    tempArray() = Split(temp, "\")
    FiltAll = tempArray(UBound(tempArray))
       
End Function
Public Function Reset_IsSwept()
       For i = 0 To ItemNum - 1
            DevConf(i).IsSwept = False
       Next i
End Function
'change on 2012-04-17 by songchunming
Public Function SearchMatch(item As ItemConf) As Integer
    
    Dim tempString As String
    Dim dec As Double
    Dim cop As String
    tempString = GetName(item.ItemName, "&", "last")
    cop = GetName(tempString, "#", "first")

    For i = 0 To ItemNum - 1
        If (GetName(DevConf(i).ItemName, "&", "last") = cop) Then
            SearchMatch = i
            Exit Function
        End If
    Next i
    MsgBox "No matched item can be found!", vbOKOnly + vbCritical, "Error!"
    
End Function
'add at 2012-04-17 by songchunming
 Public Function SearchMatchValue(name As String) As Double
 
    Dim tempString As String
    tempString = GetName(name, "#", "last")
    If IsNumeric(tempString) = False Then
        SearchMatchValue = 0
        Exit Function
    End If
    SearchMatchValue = CDbl(tempString)
    
 End Function
 Private Function GetName(myName As String, spString As String, upDown As String) As String
    
    Dim temp() As String
    temp() = Split(myName, spString)
    If upDown = "Last" Or upDown = "L" Or upDown = "last" Then
        GetName = temp(UBound(temp))
    Else 'If upDown = "D" Then
        GetName = temp(LBound(temp))
    End If
    
 End Function

Public Sub GetWrOrder()

    For i = 0 To ItemNum - 1
        For j = 0 To ItemNum - 1
            If WriteIndex(j) = i + 1 Then
                RealWriteIndex(i) = j
                Exit For
             End If
         Next j
    Next i

End Sub
'to get the maxium number in array WriteIndex :"n-WrIndex"'s max
Public Function MaxOrder() As Integer

    MaxOrder = WriteIndex(0)
    For i = 1 To ItemNum - 1
         If MaxOrder < WriteIndex(i) Then
            MaxOrder = WriteIndex(i)
         End If
    Next i
    
End Function
'This Function is designed to conduct the toal result
Public Function TotalJug(Aray() As Boolean, Length As Integer) As String
       
       Dim TempResult As Boolean
       TotalJug = "Fail"
       TempResult = True
       For i = 0 To Length
            TempResult = Aray(i) And TempResult
            If (TempResult = False) Then
                Exit For
            End If
       Next i
       
       If (TempResult = True) Then
            TotalJug = "Pass"
       Else
            TotalJug = "Fail"
       End If
       
End Function

Public Sub JugSetTrue(Aray() As Boolean, Length As Integer)
       For i = 0 To Length - 1
            Aray(i) = True
       Next i
End Sub

'replace "\" to "." and makes compact with rules of naming folders
Public Function GetDateStr(ByVal DstStr As String) As String
    Dim pos As Integer
    For i = 0 To 1
        pos = InStr(DstStr, "/")
        If (pos <> 0) Then
            Mid(DstStr, pos, 1) = "."
        End If
    Next i
    GetDateStr = DstStr
End Function
'add on 2012-03-06 songchunming
Function GetTypeName(name As String) As String
    
    Dim temp() As String
    Dim flag(1) As Long
    Dim splitChar(1) As String
    GetTypeName = "NS"
    temp = Split(name, "_")
    flag(0) = SafeArrayGetDim(temp)
    flag(1) = UBound(temp) - LBound(temp)
    If flag(0) <= 0 Or flag(1) <= 0 Or Len(temp((0))) = 0 Then
        Exit Function
    End If
    GetTypeName = temp(0)
    
End Function

'add at 2012-03-13 songchunming
'for cal data is expiration or not
Public Function Check_Cal(Optional tempData As Integer = 3) As Boolean
       
       Check_Cal = True
       Dim Modify_Time() As Date
       ReDim Modify_Time(StateNum - 1) As Date
       For i = 0 To StateNum - 1
            Modify_Time(i) = FileDateTime(stateName(i))
       Next i
       Dim Date_Now As Date
       Date_Now = Date

       Dim counter_origin As Integer
       counter = 0
       A = Date_Now - Modify_Time(0)
       For i = 0 To StateNum - 1
           If Date_Now - Modify_Time(i) >= tempData Then
              counter_origin = counter_origin + 1
           End If
       Next i
       Erase Modify_Time
       If counter_origin = 0 Then
          Check_Cal = True
       Else
          Check_Cal = False
       End If
       
End Function
'add at 2012-05-08 by songchunming
Public Function NameCheck(NameString As String, NameLong As Integer, NameShort As Integer) As Boolean

    Dim NameFirst As Integer
    NameCheck = True
    'trim space
    NameString = Trim(NameString)
    NameFirst = Asc(Left(NameString, 1))
    'sn long
    If Len(NameString) > NameLong Then
        NameCheck = False
        MsgBox "The SN is too long!", vbOKOnly + vbCritical, "Error!"
        Exit Function
    End If
    If Len(NameString) < NameShort Then
        NameCheck = False
        MsgBox "The SN is too short!", vbCritical + vbOKOnly, "Error!"
        Exit Function
    End If
    'sn fisrt letter
    If NameFirst < 48 Or NameFirst > 57 And NameFirst < 65 Or NameFirst > 90 And NameFirst < 97 Or NameFirst > 122 Then
        NameCheck = False
        MsgBox "The SN first Letter is Wrong!", vbCritical + vbOKOnly, "Error!"
    End If

End Function
'2013-05-22 by songchunming
'SAVE max And Min
Sub SaveToFirst(ByVal k As Integer, ByVal des As Integer)
    
'    If k <> des Then
'        If (DevConf(des).max < DevConf(k).max) Then
'            DevConf(des).max = DevConf(k).max
'        End If
'        If DevConf(des).min > DevConf(k).min Then
'            DevConf(des).min = DevConf(k).min
'        End If
'    End If
    
End Sub
'2013-05-22 by songchunming
'get offset
Sub SaveMUXOffset(ByVal k As Integer, ByVal des As Integer)
    If k <> des Then
        If (DevConf(des).max < DevConf(k).max) Then
            DevConf(des).max = DevConf(k).max
        End If
        If DevConf(des).min > DevConf(k).min Then
            DevConf(des).min = DevConf(k).min
        End If
    End If
    DevConf(k).max = DevConf(des).max - DevConf(des).min
    DevConf(k).min = DevConf(k).max
End Sub
Function ReturnPH(MyList As Collection, max As Double, min As Double) As Double
    
    Dim myLen As Integer
    Dim flagP As Boolean
    Dim flagM As Boolean
    flagM = False
    flagP = False
    Dim temp
    myLen = MyList.count
    For i = 1 To myLen
        If MyList.item(i) > 170 Then
            flagP = True
        ElseIf MyList.item(i) < -170 Then
            flagM = True
        End If
    Next
    If flagP = True And flagM = True Then
         For i = 1 To myLen
            If MyList(1) > 0 Then
                temp = MyList(1) - 360
            Else
                temp = MyList(1)
            End If
            MyList.Remove 1
            MyList.Add temp
         Next i
    End If
    max = MyList(1)
    min = MyList(1)
    For i = 2 To myLen
        If MyList(i) > max Then
            max = MyList(i)
        End If
        If MyList(i) < min Then
            min = MyList(i)
        End If
    Next i
    For i = 1 To MyList.count
        MyList.Remove 1
    Next i
    ReturnPH = max - min
    
End Function
